home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / program / fpk65_66.zip / SOURCE / RTL / DOS / SYSTEM.PP < prev    next >
Text File  |  1997-02-11  |  14KB  |  580 lines

  1. {****************************************************************************
  2.  
  3.                    Copyright (c) 1993,96 by Florian Klaempfl
  4.  
  5.  ****************************************************************************}
  6.  
  7. { Unit System für DOS-Extender von DJ Delorie }
  8. {$define DOS}
  9. unit system;
  10.  
  11.   interface
  12.  
  13.     { die betriebssystemunabhangigen Deklarationen einfuegen: }
  14.  
  15.     {$I SYSTEMH.INC}
  16.     
  17.     {$I HEAPH.INC}
  18.  
  19.   implementation
  20.  
  21.     { die betriebssystemunabhängigen Implementationen einfuegen: }
  22.  
  23.     {$I SYSTEM.INC}
  24.  
  25.     type
  26.        plongint = ^longint;
  27.  
  28.     procedure halt;
  29.  
  30.       begin
  31.          asm
  32.             movl $0x4c00,%eax
  33.             int $0x21
  34.          end;
  35.       end;
  36.  
  37.     procedure halt(errnum : byte);
  38.  
  39.       begin
  40.          do_exit;
  41.          asm
  42.             movl $0x4c00,%eax
  43.             movb 8(%ebp),%al
  44.             int $0x21
  45.          end;
  46.       end;
  47.  
  48.     function paramcount : longint;
  49.  
  50.       begin
  51.          asm
  52.             movl _argc,%eax
  53.             decl %eax
  54.             leave
  55.             ret
  56.          end ['EAX'];
  57.       end;
  58.  
  59.     function paramstr(l : longint) : string;
  60.  
  61.       function args : pointer;
  62.  
  63.         begin
  64.            asm
  65.               movl _args,%eax
  66.               leave
  67.               ret
  68.            end ['EAX'];
  69.         end;
  70.  
  71.       var
  72.          p : ^pchar;
  73.  
  74.       begin
  75.          if (l>=0) and (l<=paramcount) then
  76.            begin
  77.               p:=args;
  78.               paramstr:=strpas(p[l]);
  79.            end
  80.          else paramstr:='';
  81.       end;
  82.  
  83.     procedure randomize;
  84.  
  85.       var
  86.          hl : longint;
  87.  
  88.       begin
  89.          asm
  90.             movb $0x2c,%ah
  91.             int $0x21
  92.             movw %cx,-4(%ebp)
  93.             movw %dx,-2(%ebp)
  94.          end;
  95.          randseed:=hl;
  96.       end;
  97.  
  98. { use standard heap management }
  99. {$I HEAP.INC}
  100.  
  101. {****************************************************************************
  102.                     Unterprogramme zu Dateiverwaltung
  103.  ****************************************************************************}
  104.  
  105.     procedure do_close(h : longint);
  106.  
  107.       begin
  108.          asm
  109.             movl 8(%ebp),%ebx
  110.             movb $0x3e,%ah
  111.             pushl %ebp
  112.             intl $0x21
  113.             popl %ebp
  114.          end;
  115.       end;
  116.  
  117.     procedure fileclosefunc(var t : textrec);
  118.  
  119.       begin
  120.          do_close(t.handle);
  121.       end;
  122.  
  123.     function open(f : pchar;flags : longint) : longint;
  124.  
  125.       begin
  126.          asm
  127.             movw $0xff02,%ax
  128.         movl 8(%ebp),%ebx
  129.             movl 12(%ebp),%ecx
  130.             int $0x21
  131.             jnc LOPEN1
  132.             movw %ax,U_SYSTEM_INOUTRES;
  133.             xorl %eax,%eax
  134.          LOPEN1:
  135.             // Returnwert ist in EAX
  136.             leave
  137.             ret $8
  138.          end;
  139.       end;
  140.  
  141.     procedure doserase(p : pchar);
  142.  
  143.       begin
  144.          asm
  145.             movl 8(%ebp),%edx
  146.             movb $0x41,%ah
  147.             pushl %ebp
  148.             int $0x21
  149.             popl %ebp
  150.             jnc LERASE1
  151.             movw %ax,U_SYSTEM_INOUTRES;
  152.          LERASE1:
  153.          end;
  154.       end;
  155.  
  156.     procedure dosrename(p1,p2 : pchar);
  157.  
  158.       begin
  159.          asm
  160.             movl 8(%ebp),%edx
  161.             movl 12(%ebp),%edi
  162.             movb $0x56,%ah
  163.             pushl %ebp
  164.             int $0x21
  165.             popl %ebp
  166.             jnc LRENAME1
  167.             movw %ax,U_SYSTEM_INOUTRES;
  168.          LRENAME1:
  169.          end;
  170.       end;
  171.  
  172.     procedure doswrite(h,addr,len : longint);
  173.  
  174.       begin
  175.          asm
  176.             movl 16(%ebp),%ecx
  177.             movl 12(%ebp),%edx
  178.             movl 8(%ebp),%ebx
  179.             movb $0x40,%ah
  180.             int $0x21
  181.             jnc LDOSWRITE1
  182.             movw %ax,U_SYSTEM_INOUTRES;
  183.          LDOSWRITE1:
  184.          end;
  185.       end;
  186.  
  187.     function dosread(h,addr,len : longint) : longint;
  188.  
  189.       begin
  190.          asm
  191.             movl 16(%ebp),%ecx
  192.             movl 12(%ebp),%edx
  193.             movl 8(%ebp),%ebx
  194.             movb $0x3f,%ah
  195.             int $0x21
  196.             jnc LDOSREAD1
  197.             movw %ax,U_SYSTEM_INOUTRES;
  198.             xorl %eax,%eax
  199.          LDOSREAD1:
  200.             leave
  201.             ret $12
  202.          end;
  203.       end;
  204.  
  205.     function dosfilepos(handle : longint) : longint;
  206.  
  207.       begin
  208.          asm
  209.             movb $0x42,%ah
  210.             movb $0x1,%al
  211.             movl 8(%ebp),%ebx
  212.             xorl %ecx,%ecx
  213.             xorl %edx,%edx
  214.             pushl %ebp
  215.             int $0x21
  216.             popl %ebp
  217.             jnc LDOSFILEPOS1
  218.             movw %ax,U_SYSTEM_INOUTRES;
  219.             xorl %eax,%eax
  220.             jmp LDOSFILEPOS2
  221.          LDOSFILEPOS1:
  222.             shll $16,%edx
  223.             movzwl %ax,%eax
  224.             orl %edx,%eax
  225.          LDOSFILEPOS2:
  226.             leave
  227.             ret $4
  228.          end;
  229.       end;
  230.  
  231.     procedure dosseek(handle : longint;pos : longint);
  232.  
  233.       begin
  234.          asm
  235.             movb $0x42,%ah
  236.             xorb %al,%al
  237.             movl 8(%ebp),%ebx
  238.             movl 12(%ebp),%edx
  239.             // ginge auch mit SHLD
  240.             movl %edx,%ecx
  241.             shrl $16,%ecx
  242.             pushl %ebp
  243.             int $0x21
  244.             popl %ebp
  245.             jnc LDOSSEEK1
  246.             movw %ax,U_SYSTEM_INOUTRES;
  247.          LDOSSEEK1:
  248.          end;
  249.       end;
  250.  
  251.     function dosfilesize(handle : longint) : longint;
  252.  
  253.       function set_at_end(handle : longint) : longint;
  254.  
  255.         begin
  256.            asm
  257.               movb $0x42,%ah
  258.               movb $0x2,%al
  259.               // Vorsicht Stack: 0 %ebp; 4 retaddr;
  260.               // 8 nextstackframe; 12 handle
  261.               movl 12(%ebp),%ebx
  262.               xorl %ecx,%ecx
  263.               xorl %edx,%edx
  264.               pushl %ebp
  265.               int $0x21
  266.               popl %ebp
  267.               jnc Lset_at_end1
  268.               movw %ax,U_SYSTEM_INOUTRES;
  269.               xorl %eax,%eax
  270.               jmp Lset_at_end2
  271.            Lset_at_end1:
  272.               shll $16,%edx
  273.               movzwl %ax,%eax
  274.               orl %edx,%eax
  275.            Lset_at_end2:
  276.               leave
  277.               ret $8
  278.            end;
  279.          end;
  280.  
  281.       var
  282.          tempfilesize : longint;
  283.          aktfilepos : longint;
  284.  
  285.       begin
  286.          aktfilepos:=dosfilepos(handle);
  287.          tempfilesize:=set_at_end(handle);
  288.          dosseek(handle,aktfilepos);
  289.          dosfilesize:=tempfilesize;
  290.       end;
  291.  
  292.     procedure fileopenfunc(var f : textrec);
  293.  
  294.       var
  295.          b : array[0..255] of char;
  296.  
  297.       begin
  298.          move(f.name[1],b,length(f.name));
  299.          b[length(f.name)]:=#0;
  300.          f.inoutfunc:=@fileinoutfunc;
  301.          f.flushfunc:=@fileinoutfunc;
  302.          f.closefunc:=@fileclosefunc;
  303.          case f.mode of
  304.             fminput : f.handle:=open(b,$8001);
  305.             fmoutput : f.handle:=open(b,$8302);
  306.             fmappend : begin
  307.                           f.handle:=open(b,$8902);
  308.                           f.mode:=fmoutput;
  309.                        end;
  310.          end;
  311.       end;
  312.  
  313.     function eof(var t : text) : boolean;[iocheck];
  314.  
  315.       begin
  316.          eof:=dosfilesize(textrec(t).handle)<=dosfilepos(textrec(t).handle);
  317.          if eof then
  318.            eof:=textrec(t).bufend<=textrec(t).bufpos;
  319.       end;
  320.  
  321.     procedure rewrite(var f : file;l : word);[iocheck];
  322.  
  323.       var
  324.          b : array[0..255] of char;
  325.  
  326.       begin
  327.          filerec(f).mode:=fmoutput;
  328.          move(filerec(f).name[1],b,length(filerec(f).name));
  329.          b[length(filerec(f).name)]:=#0;
  330.        filerec(f).handle:=open(b,$8302);
  331.        filerec(f).recsize:=l;
  332.       end;
  333.  
  334.     procedure reset(var f : file;l : word);[iocheck];
  335.  
  336.       var
  337.          b : array[0..255] of char;
  338.  
  339.       begin
  340.          move(filerec(f).name[1],b,length(filerec(f).name));
  341.          b[length(filerec(f).name)]:=#0;
  342.          {
  343.            filerec(f).mode:=fminput;
  344.            filerec(f).handle:=open(b,$8001);
  345.          }
  346.          case filemode of
  347.             0 : begin
  348.                    filerec(f).mode:=fminput;
  349.                    filerec(f).handle:=open(b,$8001);
  350.                 end;
  351.             1 : begin
  352.                    filerec(f).mode:=fmoutput;
  353.                    filerec(f).handle:=open(b,$8302);
  354.                 end;
  355.             2 : begin
  356.                    filerec(f).mode:=fminout;
  357.                    filerec(f).handle:=open(b,$8404);
  358.                 end;
  359.          end;
  360.        filerec(f).recsize:=l;
  361.       end;
  362.  
  363.     procedure rewrite(var f : file);[iocheck];
  364.  
  365.        begin
  366.           rewrite(f,128);
  367.        end;
  368.  
  369.     procedure reset(var f : file);[iocheck];
  370.  
  371.        begin
  372.           reset(f,128);
  373.        end;
  374.  
  375.     procedure blockwrite(var f : file;var buf;count : longint);[iocheck];
  376.  
  377.        var
  378.           p : pointer;
  379.           size : longint;
  380.  
  381.         begin
  382.            p:=@buf;
  383.            doswrite(filerec(f).handle,longint(p),count*filerec(f).recsize);
  384.         end;
  385.  
  386.     procedure blockread(var f : file;var buf;count : longint;var result : longint);[iocheck];
  387.  
  388.       begin
  389.          result:=dosread(filerec(f).handle,longint(@buf),
  390.            count*filerec(f).recsize) div filerec(f).recsize;
  391.       end;
  392.  
  393.     procedure blockread(var f : file;var buf;count : longint);[iocheck];
  394.  
  395.       var
  396.          result : longint;
  397.  
  398.       begin
  399.          blockread(f,buf,count,result);
  400.       end;
  401.  
  402.     function filepos(var f : file) : longint;[iocheck];
  403.  
  404.       begin
  405.          filepos:=dosfilepos(filerec(f).handle) div filerec(f).recsize;
  406.       end;
  407.  
  408.     function filesize(var f : file) : longint;[iocheck];
  409.  
  410.       begin
  411.          filesize:=dosfilesize(filerec(f).handle) div filerec(f).recsize;
  412.       end;
  413.  
  414.     function eof(var f : file) : boolean;[iocheck];
  415.  
  416.       begin
  417.          eof:=filesize(f)<=filepos(f);
  418.       end;
  419.  
  420.     procedure seek(var f : file;pos : longint);[iocheck];
  421.  
  422.       begin
  423.          dosseek(filerec(f).handle,pos*filerec(f).recsize);
  424.       end;
  425.  
  426.     procedure close(var f : file);[iocheck];
  427.  
  428.       begin
  429.          if (filerec(f).mode<>fmclosed) then
  430.            begin
  431.               filerec(f).mode:=fmclosed;
  432.               do_close(filerec(f).handle);
  433.            end;
  434.       end;
  435.       
  436.     procedure dos_dirs(func : byte;name : pchar);
  437.     
  438.       begin
  439.          asm
  440.             movl 10(%ebp),%edx
  441.             movb 8(%ebp),%ah
  442.             int $0x21
  443.             jnc LDOS_DIRS1
  444.             movw %ax,U_SYSTEM_INOUTRES;
  445.          LDOS_DIRS1:
  446.             leave
  447.             ret $6
  448.          end;
  449.       end;
  450.  
  451.     procedure _dir(func : byte;const s : string);
  452.     
  453.       var
  454.          buffer : array[0..255] of char;
  455.  
  456.       begin
  457.          move(s[1],buffer,length(s));
  458.          buffer[length(s)]:=#0;
  459.          dos_dirs(func,buffer);
  460.       end;
  461.  
  462.     procedure mkdir(const s : string);
  463.  
  464.       begin
  465.          _dir($39,s);
  466.       end;
  467.  
  468.     procedure rmdir(const s : string);
  469.  
  470.       begin
  471.          _dir($3a,s);
  472.       end;
  473.  
  474.     procedure chdir(const s : string);
  475.  
  476.       begin
  477.          _dir($3b,s);
  478.       end;
  479.  
  480.     { thanks to Michael Van Canneyt <michael@tfdec1.fys.kuleuven.ac.be>, }
  481.     { who writes this code                                               }
  482.     procedure getdir(drivenr : byte;var dir : string);
  483.  
  484.       var
  485.          temp : string;
  486.          sof : pointer;
  487.          i : byte;
  488.  
  489.       begin
  490.          sof:=@dir[4];
  491.  
  492.          { dir[1..3] will contain '[drivenr]:\', but is not }
  493.          { supplied by DOS, so we let dos string start at   }
  494.          { dir[4]                                           }
  495.          asm
  496.             { Get dir from drivenr : 0=default, 1=A etc... }
  497.             movb drivenr,%dl
  498.  
  499.             { put (previously saved) offset in si }
  500.             movl sof,%esi
  501.  
  502.             { call msdos function 47H : Get dir }
  503.             mov $0x47,%ah
  504.  
  505.             { make the call }
  506.             int $0x21
  507.  
  508.             { Rem: if call unsuccesfull, carry is set, and AX has }
  509.             { error code                                          }
  510.  
  511.  
  512.          end;
  513.          { Now Dir should be filled with directory in ASCIIZ, }
  514.          { starting from dir[4]                               }
  515.          dir[0]:=#3;
  516.          dir[2]:=':';
  517.          dir[3]:='\';
  518.  
  519.          i:=4;
  520.  
  521.          { conversation to Pascal string }
  522.          while (dir[i]<>#0) do
  523.            begin
  524.               { convert path name to DOS }
  525.               if dir[i]='/' then
  526.                 dir[i]:='\';
  527.               dir[0]:=chr(i);
  528.               inc(i);
  529.            end;
  530.          { upcase the string (FPKPascal function) }
  531.          dir:=upcase(dir);
  532.          if drivenr<>0 then   { Drive was supplied. We know it }
  533.            dir[1]:=chr(65+drivenr-1)
  534.          else
  535.            begin
  536.               { We need to get the current drive from DOS function 19H  }
  537.               { because the drive was the default, which can be unknown }
  538.               asm
  539.                  movb $0x19,%ah
  540.                  int $0x21
  541.                  addb $65,%al
  542.                  movb %al,i
  543.               end;
  544.               dir[1]:=chr(i)
  545.            end;
  546.       end;
  547.  
  548.   var
  549.      i : longint;
  550.  
  551. begin
  552.    exitproc:=nil;
  553.    { Heapmanagement initialisieren }
  554.    {
  555.    for i:=1 to 32 do
  556.      blocks[i]:=nil;
  557.    }
  558.    heaporg:=getheapstart;
  559.    heapptr:=heaporg;
  560.    _memavail:=getheapsize;
  561.    heapend:=heaporg+_memavail;
  562.    heaperror:=nil;
  563.    freelist:=nil;
  564.    { Standartinput initialisieren }
  565.    assign(input,'');
  566.    textrec(input).handle:=0;
  567.    textrec(input).mode:=fminput;
  568.    textrec(input).inoutfunc:=@fileinoutfunc;
  569.    textrec(input).flushfunc:=@fileinoutfunc;
  570.    { Standartoutput initialisieren }
  571.    assign(output,'');
  572.    textrec(output).handle:=1;
  573.    textrec(output).mode:=fmoutput;
  574.    textrec(output).inoutfunc:=@fileinoutfunc;
  575.    textrec(output).flushfunc:=@fileinoutfunc;
  576.    textrec(input).mode:=fminput;
  577.    { kein Ein- Ausgabefehler }
  578.    inoutres:=0;
  579. end.
  580.